package com.weed.table.model;

import java.io.Serializable;
import java.util.Vector;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import org.util.grid.DataGrid;
import org.util.grid.event.ColumnChangeEvent;
import org.util.grid.event.CursorMovedEvent;
import org.util.grid.event.DataGridEvent;
import org.util.grid.event.DataGridListener;
import org.util.grid.event.RowChangeEvent;
import org.util.grid.event.ValueChangeEvent;
/**
 * 使用DataGrid用于存储核心的数据。
 * @author Jason
 */
public class DataGridTableModel extends AbstractTableModel implements Serializable,DataGridListener {


    protected DataGrid dataGrid = new DataGrid();

    public DataGridTableModel() {
        super();
    }

    public DataGridTableModel(DataGrid dataGrid) {
        super();
        this.dataGrid = dataGrid;
        if(dataGrid!=null)
            dataGrid.addDataGridListener(this);
    }

    public DataGrid getDataGrid() {
        return dataGrid;
    }

    public void setDataVector(DataGrid dataGrid) {
        fireTableStructureChanged();
    }

    public void newDataAvailable(TableModelEvent event) {
        fireTableChanged(event);
    }

    public void addRow(List rowData) {
        this.dataGrid.addRow(rowData);
    }
    //newRow

    public void newRowsAdded(TableModelEvent e) {
        int column = this.getColumnCount();
        Object[] obj = new Object[column];
        for (int i = 0; i < this.getRowCount(); i++) {
            obj[i] = new String("");
        }
        dataGrid.addRow(obj);
        fireTableChanged(e);
    }

    public void rowsRemoved(TableModelEvent event) {
        fireTableChanged(event);
    }

    public void setNumRows(int rowCount) {
        this.dataGrid.setMaxRows(rowCount);
    }

    public void setRowCount(int rowCount) {
        dataGrid.setMaxRows(rowCount);
    }

    public void addRow(Object[] rowData) {
        dataGrid.addRow(rowData);
    }

    public void moveRow(int start, int end, int to) {
        System.out.println("start=" + start + ";end=" + end + ";to=" + to);
    }

    public void removeRow(int row) {
        dataGrid.removeRow(row);
    }

    public void setColumnCount(int columnCount) {
        fireTableStructureChanged();
    }
    /**
    public void addColumn(Object columnName) {
        addColumn(columnName, (Vector) null);
    }

    public void addColumn(Object columnName, Vector columnData) {
        addColumn(columnName,(Object[])null);
    }

    public void addColumn(Object columnName, Object[] columnData) {
       
    }
    */
    public int getRowCount() {
        return dataGrid.getRowCount();
    }

    public int getColumnCount() {
        return dataGrid.getColumnCount();
    }

    public String getColumnName(int column) {
        return dataGrid.getColumnName(column);
    }
 
    public boolean isCellEditable(int row, int column) {
        return true;
    }

    public Object getValueAt(int row, int column) {
        return dataGrid.getValueAt(row, column);
    }

    public void setValueAt(Object aValue, int row, int column) {
        dataGrid.setValueAt(row, column, aValue);
    }

    public Class getColumnClass(int column) {
        Class returnValue = null;
        if ((column >= 0) && (column < getColumnCount())) {
            returnValue = getValueAt(0, column).getClass();
        } else {
            returnValue = Object.class;
        }
        return returnValue;
    }

    public void dataGridChange(DataGridEvent event) {
        //fireTableDataChanged();
    }

    public void rowChange(RowChangeEvent event) {
        fireTableDataChanged();
    }

    public void cursorMoved(CursorMovedEvent event) {
        //fireTableDataChanged();
    }

    public void valueChange(ValueChangeEvent event) {
        fireTableDataChanged();
    }

    public void columnChange(ColumnChangeEvent event) {
        fireTableDataChanged();
    }
}
